package com.zy.explore.config;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * description Swagger自动配置属性
 * 
 * @author ZY
 * @date 2021-05-05 17:53
 */
@Data
@Validated
@ConditionalOnProperty(value = SwaggerProperties.ENABLED, matchIfMissing = true)
@ConfigurationPropertiesScan(SwaggerProperties.PREFIX)
public class SwaggerProperties {

    static final String PREFIX = "spring.swagger";

    static final String ENABLED = PREFIX + ".enabled";
    /**
     * 错误代码
     */
    @NotNull
    private final List<@NotNull Class<?>> codes = new ArrayList<>();
    /**
     * 联系人信息
     */
    @Valid
    private final Contact contact = new Contact();
    /**
     * swagger会解析的url规则
     */
    @Valid
    private final List<@NotBlank String> basePath = new ArrayList<>();
    /**
     * 在basePath基础上需要排除的url规则
     */
    @Valid
    private final List<@NotBlank String> excludePath = new ArrayList<>();
    /**
     * 分组文档
     */
    @Valid
    private final Map<String, @Valid GroupInfo> groups = new LinkedHashMap<>();
    /**
     * swagger2全局参数配置
     */
    @Valid
    private final List<@Valid GlobalParameter> parameters = new ArrayList<>();
    /**
     * 是否开启swagger
     */
    @AssertTrue
    private boolean enabled = true;
    /**
     * 是否开启验证插件支持（默认关闭）
     */
    private boolean validatorPlugin = false;
    /**
     * 标题
     */
    @NotNull
    private String title = "";
    /**
     * 描述
     */
    @NotNull
    private String description = "";
    /**
     * 版本
     */
    @NotNull
    private String version = "";
    /**
     * 许可证
     */
    @NotNull
    private String license = "";
    /**
     * 许可证URL
     */
    @NotNull
    private String licenseUrl = "";
    /**
     * 服务条款URL
     */
    @NotNull
    private String termsOfServiceUrl = "";
    /**
     * swagger会解析的包路径
     */
    @NotNull
    private String basePackage = "";
    /**
     * host信息
     */
    @NotNull
    private String host = "";

    @Data
    @NoArgsConstructor
    public static final class GroupInfo {

        /**
         * 联系人信息
         */
        @Valid
        private final Contact contact = new Contact();
        /**
         * swagger会解析的url规则
         */
        @Valid
        private final List<@NotBlank String> basePath = new ArrayList<>();
        /**
         * 在basePath基础上需要排除的url规则
         */
        @Valid
        private final List<@NotBlank String> excludePath = new ArrayList<>();
        /**
         * 标题
         */
        @NotNull
        private String title = "";
        /**
         * 描述
         */
        @NotNull
        private String description = "";
        /**
         * 版本
         */
        @NotNull
        private String version = "";
        /**
         * 许可证
         */
        @NotNull
        private String license = "";
        /**
         * 许可证URL
         */
        @NotNull
        private String licenseUrl = "";
        /**
         * 服务条款URL
         */
        @NotNull
        private String termsOfServiceUrl = "";
        /**
         * swagger会解析的包路径
         */
        @NotNull
        private String basePackage = "";
    }

    @Data
    @NoArgsConstructor
    public static final class Contact {

        /**
         * 联系人
         */
        @NotNull
        private String name = "";

        /**
         * 联系人url
         */
        @NotNull
        private String url = "";

        /**
         * 联系人email
         */
        @NotNull
        private String email = "wenwo@pingcl.com";
    }
    
    /**
     *	swagger2全局参数配置
     *
     */
    @Data
    @NoArgsConstructor
    public static final class GlobalParameter {

        /**
         * 参数名称
         */
        @NotNull
        private String name = "token";

        /**
         * 描述
         */
        @NotNull
        private String description = "token";

        /**
         * 数据类型
         */
        @NotNull
        private String modelRef = "string";
        
        /**
         * 参数类型
         */
        @NotNull
        private String parameterType = "header";
    }
}
